﻿using System;
using System.Text;

namespace LBSoft.Common.DotNetExt
{
    public static class ExtInt32
    {
        #region 私有静态变量，及获取静态变量的方法
        private static char[] lowerArray = null;
        private static char[] upperArray = null;
        private static char[] weekDayArray = null;
        private static string[] dayArray = null;
        private static char[] monthArray = null;
        /// <summary>
        /// 获取小写中文数组
        /// </summary>
        /// <returns></returns>
        private static char[] GetLowerArray()
        {
            if (lowerArray == null)
            {
                lowerArray = new char[] { '〇', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
            }
            return lowerArray;
        }

        /// <summary>
        /// 获取大写中文数组
        /// </summary>
        /// <returns></returns>
        private static char[] GetUpperArray()
        {
            if (upperArray == null)
            {
                upperArray = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
            }
            return upperArray;
        }

        /// <summary>
        /// 获取中文星期数组
        /// </summary>
        /// <returns></returns>
        private static char[] GetWeekDayArray()
        {
            if (weekDayArray == null)
            {
                weekDayArray = new char[] { '日', '一', '二', '三', '四', '五', '六', '日' };
            }
            return weekDayArray;
        }

        /// <summary>
        /// 获取中文日期数组
        /// </summary>
        /// <returns></returns>
        private static string[] GetDayArray()
        {
            if (dayArray == null)
            {
                dayArray = new string[] {"","初一", "初二", "初三", "初四", "初五",
                                            "初六", "初七", "初八", "初九", "初十",
                                            "十一", "十二", "十三", "十四", "十五",
                                            "十六", "十七", "十八", "十九", "二十",
                                            "廿一", "廿二", "廿三", "廿四", "廿五",
                                            "廿六", "廿七", "廿八", "廿九", "三十"};
            }
            return dayArray;
        }

        /// <summary>
        /// 获取中文日期数组
        /// </summary>
        /// <returns></returns>
        private static char[] GetMonthArray()
        {
            if (monthArray == null)
            {
                monthArray = new char[] { ' ', '正', '二', '三', '四', '五', '六', '七', '八', '九', '十', '冬', '腊' };
            }
            return monthArray;
        }
        #endregion

        /// <summary>
        /// 将一个小于10，大于-10的数字转换成中文，超出范围会返回string.Empty
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isLower">是否是小写，如果值为false,则表示会返回大写</param>
        /// <returns></returns>
        public static string ToSingleCn(this int source, bool isLower)
        {
            char[] chineseSource = isLower ? GetLowerArray() : GetUpperArray();
            if (source < -10 || source >= 10)
            {
                return string.Empty;
            }

            if (source >= 0)
            {
                return lowerArray[source].ToString();
            }
            else
            {
                return "负" + chineseSource[Math.Abs(source)].ToString();
            }
        }

        /// <summary>
        /// 将整数转换成中文字符
        /// </summary>
        /// <param name="source"></param>
        /// <param name="isLower">是否是小写，如果值为false,则表示会返回大写</param>
        /// <returns></returns>
        public static string ToCn(this int source, bool isLower)
        {
            char[] chineseSource = isLower ? GetLowerArray() : GetUpperArray();
            StringBuilder chinese = new StringBuilder();
            if (source < 0)
            {
                chinese.Append("负");
            }

            string strSource = Math.Abs(source).ToString();
            string strNum = string.Empty;
            for (int i = 0; i < strSource.Length; i++)
            {
                strNum = strSource.Substring(i, 1);
                chinese.Append(chineseSource[Convert.ToInt32(strNum)]);
            }

            return chinese.ToString();
        }
        /// <summary>
        /// 转换成中文星期
        /// </summary>
        /// <param name="source"></param>
        /// <param name="prefix">前缀，如果为null或string.Empty,则默认前缀为星期</param>
        /// <returns></returns>
        public static string ToWeekDayCn(this int source, string prefix)
        {
            if (source < 0 || source > 7)
            {
                return string.Empty;
            }

            if (string.IsNullOrEmpty(prefix))
            {
                prefix = "星期";
            }
            char[] chineseSource = GetWeekDayArray();
            string weekDay = prefix + chineseSource[source];
            return weekDay;
        }

        /// <summary>
        /// 转换为中文日期（初一，初二.....）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToDayCn(this int source)
        {
            if (source < 1 || source > 30)
            {
                return string.Empty;
            }
            string[] chineseSource = GetDayArray();
            return chineseSource[source];
        }

        /// <summary>
        /// 转换为中文月份（正月，二月.....）
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ToMonthCn(this int source)
        {
            if (source < 1 || source > 12)
            {
                return string.Empty;
            }
            char[] chineseSource = GetMonthArray();
            return chineseSource[source] + "月";
        }
    }
}
